package com.ysyc.driver_ee.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.adapter.DriverDetailsAdapter;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseActivity;
import com.ysyc.driver_ee.factory.RecyclerViewFactory;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.model.OrderForm;
import com.ysyc.driver_ee.tools.PermissionUtils;
import com.ysyc.driver_ee.tools.StatusSettingUtils;
import com.ysyc.driver_ee.widget.SureAndCancelDialog;
import com.ysyc.driver_ee.widget.TitleView;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/30 14:47<br>
 * 版本： v1.0<br>
 */
public class DriverDetailsActivity extends BaseActivity {
    @BindView(R.id.tv_license_plate_view)
    TextView tvLicensePlateView;
    @BindView(R.id.iv_driver_view)
    ImageView ivDriverView;
    @BindView(R.id.tv_driver_name_view)
    TextView tvDriverNameView;
    @BindView(R.id.tv_driver_status_view)
    TextView tvDriverStatusView;
    @BindView(R.id.tv_location_view)
    TextView tvLocationView;
    @BindView(R.id.tv_number_of_order_view)
    TextView tvNumberOfOrderView;
    @BindView(R.id.iv_call_phone_view)
    ImageView ivCallPhoneView;
    @BindView(R.id.tv_today_view)
    TextView tvTodayView;
    @BindView(R.id.tv_accumulative_view)
    TextView tvAccumulativeView;
    @BindView(R.id.tv_single_quantity_view)
    TextView tvSingleQuantityView;
    @BindView(R.id.tv_accumulative_single_quantity_view)
    TextView tvAccumulativeSingleQuantityView;
    @BindView(R.id.rv_drive_details_view)
    RecyclerView rvDriveDetailsView;
    @BindView(R.id.title_driver_details_view)
    TitleView titleDriverDetailsView;

    private SureAndCancelDialog sureAndCancelDialog;
    private DriverInfo driverInfo1;
    private DriverDetailsAdapter driverDetailsAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_driver_details;
    }

    @Override
    protected void initView() {
        titleDriverDetailsView.setLeftImg(R.drawable.left_return_icon);
        titleDriverDetailsView.setLeftText("司机详情");
        titleDriverDetailsView.setLeftImgClick(v -> finish());
        int driverId = getIntent().getIntExtra("driverId", 0);
        initNetworkCallback(driverId);
        RecyclerViewFactory.initVerticalRvDecoration(DriverDetailsActivity.this,rvDriveDetailsView,10);
        driverDetailsAdapter = new DriverDetailsAdapter(DriverDetailsActivity.this, R.layout.rv_drive_details_item);
    }

    private void initNetworkCallback(int driverId) {
        NetworkCallback<DriverInfo> tNetworkCallback = new NetworkCallback<>(new HttpResource<DriverInfo>() {
            @Override
            public void onSuccess(DriverInfo driverInfo) {
                driverInfo1 = driverInfo;
                tvLicensePlateView.setText(driverInfo.carFormCn+driverInfo.carNumber);
                StatusSettingUtils.setDriverAvatar(ivDriverView,driverInfo.identity);
                tvDriverNameView.setText(driverInfo.driverName);
                StatusSettingUtils.setTransportStatus(tvDriverStatusView,driverInfo.transportStatus);
                tvLocationView.setText(driverInfo.driverMapName);
                tvNumberOfOrderView.setText("待完成订单:"+driverInfo.pendingOrderNum);
                tvTodayView.setText(driverInfo.todayWater+"");
                tvAccumulativeView.setText(driverInfo.totalWater+"");
                tvSingleQuantityView.setText(driverInfo.todayOrderNum+"");
                tvAccumulativeSingleQuantityView.setText(driverInfo.totalOrderNum+"");
            }

            @Override
            public void onFailure(String errorDes) {
                dismissDilog();
            }

            @Override
            public void onLoading(Dialog dialog) {
                showLoadingDialog();
            }
        });
        Observable<DriverInfo> driverInfo = RetrofitClient.getInstance().getHttpServices().getDriverInfo(driverId);
        tNetworkCallback.postRequest(driverInfo,null);

        NetworkCallback<List<OrderForm>> orderFormListNetworkCallback = new NetworkCallback<>(new HttpResource<List<OrderForm>>() {
            @Override
            public void onSuccess(List<OrderForm> orderFormList) {
                dismissDilog();
                driverDetailsAdapter.bindingList(orderFormList);
            }

            @Override
            public void onFailure(String errorDes) {
                dismissDilog();


            }

            @Override
            public void onLoading(Dialog dialog) {
                showLoadingDialog();
            }
        });
        Observable<List<OrderForm>> orderFormListObservable = RetrofitClient.getInstance().getHttpServices().orderFromRunning(driverId);
        orderFormListNetworkCallback.postRequest(orderFormListObservable,null);
    }

    public void callCaptain(View view) {
        String[] permisson = {Manifest.permission.CALL_PHONE};
        PermissionUtils.requestPermissions(DriverDetailsActivity.this, 1, permisson, mPermissionGrant);
    }

    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case 1:
                    if (sureAndCancelDialog == null) {
                        sureAndCancelDialog = new SureAndCancelDialog(DriverDetailsActivity.this);
                    }
                    sureAndCancelDialog.setTitleText("拨打"+driverInfo1.driverName+"电话").setMessageText(driverInfo1.telPhone).setSureText("拨打电话").setOnDialogClickListener(new SureAndCancelDialog.onDialogClickListener() {
                        @Override
                        public void onPositiveButtonClick(SureAndCancelDialog dialog) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_CALL);
                            intent.setData(Uri.parse("tel:" + driverInfo1.telPhone));
                            //开启系统拨号器
                            startActivity(intent);
                        }
                    });
                    sureAndCancelDialog.show();
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    }








}
